package com.oedtech.esl.repository.esl;

import com.oedtech.esl.model.esl.EslTab;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import javax.transaction.Transactional;
import java.util.Date;
import java.util.List;
import java.util.Set;

/**
 * 电子标签dao
 */
@Repository
public interface EslTabDao extends JpaSpecificationExecutor<EslTab>, JpaRepository<EslTab, String> {


    @Modifying
    @Transactional
    @Query(value = "delete from esl_tab where tinyIP in(:eslIds)")
    void deleteEsl(@Param("eslIds") List<String> eslIds);


    /**
     * 重置下发任务为0
     * @param tinyIP
     */
    @Modifying
    @Transactional
    @Query(value = "update esl_tab set download_times=0,str0=0,push_times=0 where tiny_ip=:tinyIP",nativeQuery = true)
    void resetDownTimes(@Param("tinyIP") String tinyIP);




    @Modifying
    @Transactional
    @Query(value = "delete from esl_tab  where tiny_ip in(:tinyIPs)")
    void deleteByTinyIPs(@Param("tinyIPs") List<String> tinyIPs);








    @Modifying
    @Transactional
    @Query(value = "INSERT INTO esl_tab (tiny_ip,network_IP,version_str,gid_index,firmware_id) VALUES(?1,?2,?3,?4,?5) " +
            " ON DUPLICATE KEY UPDATE network_IP=VALUES(network_IP),version_str=VALUES(version_str), " +
            "gid_index=VALUES(gid_index),firmware_id=VALUES(firmware_id)", nativeQuery = true)
    void insertOrUpdate(String tinyIP, String networkIP, String versionStr, Integer gid,Integer firmwareId);




    /**
     * 根据基站IP查找req_id
     *
     * @param networkIP
     * @return
     */
    @Query(value = "select gid_index from esl_tab where network_ip=?1 and push_times =1 and download_times<6  order by gid_index limit 60", nativeQuery = true)
    List<Integer> findGid(String networkIP);
    @Query(value = "select tiny_ip from esl_tab where gid_index =?1   and network_IP =?2 limit 1",nativeQuery = true)
    String findtinyIPByGid(Integer gid,String apid);


    @Transactional
    @Modifying
    @Query(value = "update esl_tab set login_time=now() where tiny_ip=?1",nativeQuery = true)
    int updateLoginTime(String tinyIP);

    @Transactional
    @Modifying
    @Query(value = "update esl_tab set push_times=1,str0=str0+1,download_times=1 where push_times=0 and str0<3 and download_times>1", nativeQuery = true)
    int retryImgFlash();

    @Modifying
    @Transactional
    @Query(value = "update esl_tab set download_times=download_times+1,push_times=0,download_date_time=now() where network_ip=:network_ip and gid_index in(:gid_indexs)", nativeQuery = true)
    int resetPushTimes(@Param("gid_indexs") List<Integer> gid_index, @Param("network_ip") String apid);

    @Query(value = "select gid_index from esl_tab where tiny_ip in(?1)", nativeQuery = true)
    List<Integer> findGidByTinyIP(List<String> tinyIPList);


    /**
     * 苏宁接口方案，导入待更新价签信息
     *
     * @param sid
     * @param backUrl
     * @param name
     * @param pageId
     * @param tinyIP
     * @return
     */
    @Transactional
    @Modifying
    @Query(value = "update esl_tab set sid=?1,back_url=?2,name=?3,default_page_id=?4,store_code=?6,push_times=1,download_times=1 where tiny_ip=?5")
    int updateEsl(String sid, String backUrl, String name, Integer pageId, String tinyIP,String storeCode);


    /**
     * 更新失败时将download_times置2，并更新反馈时间
     * @param tinyIP
     * @return
     */
    @Modifying
    @Transactional
    @Query(value = "update esl_tab set download_times=6,str0=0,feedback_time=now() where tiny_ip=?1",nativeQuery = true)
    int setFailUpdate(String tinyIP);

    @Modifying
    @Transactional
    @Query(value = "update esl_tab set download_times=0,str0=0,feedback_time=now() where tiny_ip=:tinyIP",nativeQuery = true)
    void setSuccessUpdate(@Param("tinyIP") String tinyIP);

    @Transactional
    @Modifying
    @Query(value = "update esl_tab set network_ip=?1,gid_index=?2 where tiny_ip=?3",nativeQuery = true)
    int updateNetworkIPAndGid(String apid,Integer gid,String tinyIP);



    @Query(value = "select * from esl_tab where network_ip=?1 and gid_index =?2 limit 1", nativeQuery = true)
    EslTab findByNetworkIPAndGid(String networkIP, Integer gid_index);


    /**
     * 刷新价签图像
     * @param tinyIPs  价签id list
     * @return
     */
    @Transactional
    @Modifying
    @Query(value = "update esl_tab set push_times=1,download_times=1 where tiny_ip in(?1)",nativeQuery = true)
    int refreshEslImg(List<String>tinyIPs);

    /**
     * 刷新价签图像
     * @param tinyIP  价签id
     * @return
     */
    @Transactional
    @Modifying
    @Query(value = "update esl_tab set push_times=1,download_times=1,network_ip=?2,gid_index=?3  where tiny_ip =?1",nativeQuery = true)
    int setPushTimeByTinyIP(String tinyIP,String apid,Integer gid);

    @Transactional
    @Modifying
    @Query(value = "update esl_tab set push_times=0 where tiny_ip=?1",nativeQuery = true)
    int resetPushTimesById(String tinyIP);

}
